package ccgl.com.model;

import java.text.ParseException;
import java.util.Date;

import ccgl.com.util.DateUtil;

public class DateItem {
	private Date starDate;
	private Date endDate;
	private DateItem itemDate = null;
	private Date tempDate = null;

	public DateItem() {

	}

	public DateItem(String starDate, String endDate) {
		try {
			this.starDate = DateUtil.parseDate(starDate);
			this.endDate = DateUtil.parseDate(endDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Date getStarDate() {
		return starDate;
	}

	public void setStarDate(Date starDate) {
		this.starDate = starDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public DateItem getNextWeek() throws ParseException {
		initItemDate();

		this.itemDate.setStarDate(this.tempDate);
		this.itemDate.setEndDate(DateUtil.getLastDayOfWeek(this.tempDate));
		if(this.itemDate.getEndDate().after(this.endDate)) {
			this.itemDate.setEndDate(this.endDate);
		}
		
		this.tempDate = DateUtil.getNextDateByDayCount(
				this.itemDate.getEndDate(), 1);
		

		return itemDate;
	}

	public DateItem getNextMonth() throws ParseException {
		initItemDate();

		this.itemDate.setStarDate(this.tempDate);
		this.itemDate.setEndDate(DateUtil.getLastDay(this.tempDate));
		if(this.itemDate.getEndDate().after(this.endDate)) {
			this.itemDate.setEndDate(this.endDate);
		}
		
		this.tempDate = DateUtil.getNextDateByDayCount(
				this.itemDate.getEndDate(), 1);

		return itemDate;
	}

	public DateItem getNextQuarter() throws ParseException {
		initItemDate();

		this.itemDate.setStarDate(this.tempDate);
		this.itemDate.setEndDate(DateUtil.getLastDateOfQuarter(this.tempDate));
		if(this.itemDate.getEndDate().after(this.endDate)) {
			this.itemDate.setEndDate(this.endDate);
		}
		
		this.tempDate = DateUtil.getNextDateByDayCount(
				this.itemDate.getEndDate(), 1);

		return itemDate;
	}

	public DateItem getNextYear() throws ParseException {
		initItemDate();

		this.itemDate.setStarDate(this.tempDate);
		this.itemDate.setEndDate(DateUtil.getLastDateOfYear(this.tempDate));
		if(this.itemDate.getEndDate().after(this.endDate)) {
			this.itemDate.setEndDate(this.endDate);
		}
		
		this.tempDate = DateUtil.getNextDateByDayCount(
				this.itemDate.getEndDate(), 1);

		return itemDate;
	}

	/**
	 * @throws ParseException
	 * 
	 */
	private void initItemDate() {
		if (this.tempDate == null) {
			this.tempDate = this.starDate;
		}
		
		if(this.itemDate == null) {
			this.itemDate = new DateItem();
		}
	}
	
	/**
	 *
	 * @return this.starDate <= this.endDate ? true : false
	 */
	public boolean checkDate() {
		return !this.starDate.after(this.endDate);
	}

	@Override
	public String toString() {
		return "DateItem [starDate=" + DateUtil.getDateStr(starDate)
				+ ", endDate=" + DateUtil.getDateStr(endDate) + "]";
	}
}
